package edu.hrbu.trade01.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hrbu.trade01.annotation.MyLog;
import edu.hrbu.trade01.entity.Role;
import edu.hrbu.trade01.service.RoleService;
import edu.hrbu.trade01.util.R;
import edu.hrbu.trade01.util.TableR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

import static edu.hrbu.trade01.util.ConstUtil.*;

/**
 * @author 徐登宇
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    // 条件分页查询
    @RequestMapping("/list")
    public TableR list(@RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "limit", defaultValue = "5") int limit,
                       @RequestParam(value = "searchParams", required = false) String nameStr) {

        Page<Role> pageModel = roleService.getPageWithCondition(page, limit, nameStr);
//        log.info("@"+pageModel);
        return TableR.ok(pageModel.getTotal(), pageModel.getRecords());
    }

    // 新增
    @MyLog(title = LOG_TITLE_SAVE, businessType = LOG_TYPE_SYS)
    @PostMapping("/save")
    public R save(@RequestBody Role role) {
        // 检查添加的角色名是否存在
        boolean f = roleService.check(role);
        if (!f) {
            throw new RuntimeException("该角色名已存在");

        }
        roleService.save(role);
        return R.ok();
    }

    // 获取详细信息
    @RequestMapping("/info")
    public R info(@RequestParam("id") Long id) {
        Role role = roleService.getById(id);
        return R.ok(role);
    }

    // 修改
    @MyLog(title = LOG_TITLE_UPDATE, businessType = LOG_TYPE_SYS)
    @PostMapping("/update")
    public R update(@RequestBody Role role) {
        // 检查该角色名是否已经存在（将自己先前的信息排除掉）
        boolean f = roleService.check(role);
        if (!f) {
            throw new RuntimeException("该角色名已存在");
        }
        roleService.updateById(role);
        return R.ok();
    }

    // 删除
    @MyLog(title = LOG_TITLE_DEL, businessType = LOG_TYPE_SYS)
    @PostMapping("/remove")
    public R remove(@RequestParam("id") Long id) {
        roleService.removeById(id);
        return R.ok();
    }

    // 批量删除
    @MyLog(title = "批量删除", businessType = LOG_TYPE_SYS)
    @PostMapping("/removeBatch")
    public R removeBatch(@RequestParam("ids") List<Long> ids) {
//        log.info("[ids]："+ids);
        roleService.removeBatchByIds(ids);
        return R.ok();
    }

    // 获取所有角色以及所拥有的角色
    @RequestMapping("/pre/assign")
    public R preAssign(@RequestParam("userId") Long userId) {
        HashMap<String, List<Role>> map = roleService.getRoleMapByUserId(userId);
        return R.ok(map);
    }

    @MyLog(title = "分配角色", businessType = LOG_TYPE_SYS)
    @PostMapping("/to/assign")
    public R toAssign(@RequestParam("ids") List<Long> ids,
                      @RequestParam("userId") Long userId) {
        roleService.toAssign(ids, userId);
        return R.ok();
    }


}
